
function GridTree(tableName){
	this.upRowPool = null;
	this.initBindTable(tableName);
	this.imgPath;
	this.colIndex = -1;
	this.topIndex = 1;
	this.selfId = "id";
	this.upId = "up";
	this.allFind = false;
	this.isOrder = false;
	this.indention = 20;
}
GridTree.prototype.setImgPath = function(imgPath){
	this.imgPath = imgPath;
}
GridTree.prototype.setColIndex = function(colIndex){
	this.colIndex = colIndex;
}
GridTree.prototype.setTopIndex = function(topIndex){
	this.topIndex = topIndex;
}
GridTree.prototype.setSelfId = function(selfId){
	this.selfId = selfId;
}
GridTree.prototype.setUpId = function(upId){
	this.upId = upId;
}
GridTree.prototype.setAllFind = function(allFind){
	this.allFind = allFind;
}
GridTree.prototype.setIsOrder = function(isOrder){
	this.isOrder = isOrder;
}
GridTree.prototype.setIndention = function(indention){
	this.indention = indention;
}
GridTree.prototype.createGridTreeTable = function (tableName){
	
	this.initBindTable(tableName);
	if(this.isOrder){
		this.initOrderTable();
	}
	this.initLayoutTable();
	
	
}
GridTree.prototype.initBindTable = function(tableName){
	if(tableName && tableName != undefined){
		this.gridTreeObj = document.getElementById(tableName);
	}
}
GridTree.prototype.initLayoutTable = function (){
	var rows = this.getRows();
	for(var i = this.topIndex; i < rows.length; i++){					
		var row = rows[i];
		var up = row.getAttribute(this.upId);
		var id = row.getAttribute(this.selfId);
		var isOpen = true;
		if(row.getAttribute("isOpen") != "0"){
			isOpen = false;
		}
		var cols = row.cells;
		var col = null;
		if(this.colIndex != -1){
			col = cols[this.colIndex];
		}else{
			for(var j = 0; j < cols.length; j++){
				col = cols[j];
				if(col.getAttribute("treeCol") == "1"){
					this.colIndex = j;
					break;
				}
			}
		}
		var upRow = this.findRow(up);
		var tmpPadd = 0;
		if(upRow != null){
			var upCol = upRow.cells[this.colIndex];
			var upPadd = upCol.style.paddingLeft;
			if(upPadd.indexOf("px") != -1){
				upPadd = upPadd.split("px")[0];
			}
			if(isNaN(upPadd)){
				upPadd = 0;
			}
			var tmpPadd = this.indention + parseInt(upPadd);
		}
		var isExistChild = this.isChild(id);
		var iec = "0";
		var fm = "jian.gif";
		var sm = "open.gif";
		var pointerFlag = "pointer";
		if(isExistChild){
			if(isOpen){
				fm = "jia.gif"
				sm = "close.gif";
			}
			iec = "1"
		}else{
			fm = "no.gif";
			sm = "close.gif"
			pointerFlag = "";
		}
		row.setAttribute("isExistChild", iec);
		var tmpImg = document.createElement("img");
		tmpImg.src = this.imgPath + fm;
		tmpImg.style.cursor = pointerFlag ;
		var imgOut = this.outerHTML(tmpImg);
		var tmpIoc = document.createElement("img");
		tmpIoc.src = this.imgPath + sm;
		tmpIoc.style.cursor = pointerFlag ;
		var iocOut = this.outerHTML(tmpIoc);
		col.innerHTML = imgOut + iocOut + col.innerHTML;
		this.bind(col.firstChild, "click", this.imgClick());
		this.bind(col.firstChild.nextSibling, "click", this.imgClick());
		col.style.paddingLeft = tmpPadd;
		this.doDisplay(row, isOpen);
	}
}
GridTree.prototype.imgClick = function (){
	var tmpTalbe = this;
	return function (event){
		evt = event?event:(window.event?window.event:null); 
		if(evt != null){
			var srcElement = null ;
			if(navigator.appName.indexOf("Explorer") > -1){ 
				srcElement = event.srcElement;
			}else{
				srcElement = event.target;
			}
			//图片.td.tr.tbody.table
			var trTmp = srcElement.parentNode.parentNode;
			var tId = trTmp.parentNode.parentNode.getAttribute("id");
			var isChildClose = true;
			var col = trTmp.cells[tmpTalbe.colIndex];
			var isOpen = trTmp.getAttribute("isOpen")
			if(isOpen != "0"){
				var isExistChild = trTmp.getAttribute("isExistChild");
				if(isExistChild != "0"){
					trTmp.setAttribute("isOpen", "0");
					col.firstChild.src = tmpTalbe.imgPath + "jia.gif";
					col.firstChild.nextSibling.src = tmpTalbe.imgPath + "close.gif";
				}else{
					return;
				}
			}else{
				trTmp.setAttribute("isOpen", "1");
				
				col.firstChild.src = tmpTalbe.imgPath +"jian.gif";
				col.firstChild.nextSibling.src = tmpTalbe.imgPath + "open.gif";
				
				isChildClose = false;
			}
			
			tmpTalbe.doDisplay(trTmp, isChildClose);
			
		}
	}
	
}
GridTree.prototype.doDisplay = function(row, isChildClose){

	var rowArr = this.findChilds(row);
	for(var i = 0; i < rowArr.length; i++){
		if(isChildClose){
			rowArr[i].style.display = "none";
		}else{
			if(row.getAttribute("isOpen") != "0"){
				rowArr[i].style.display = "";
			}else{
				rowArr[i].style.display = "none";
			}
		}
		
		if(rowArr[i].getAttribute("isExistChild") != "0"){
			if(rowArr[i].getAttribute("isOpen") != "0"){
				this.doDisplay(rowArr[i], isChildClose);
			}
		}
		
	}
}
GridTree.prototype.bind = function (obj, eveName, funName, flag){
	
	if(navigator.appName.indexOf("Explorer") > -1){ 
		obj.attachEvent("on"+eveName, funName);
	}else{
		obj.addEventListener(eveName, funName, flag);
	}
}

GridTree.prototype.initOrderTable = function (){
	var rows = this.getRows();
	for(var i = this.topIndex; i < rows.length; i++){					
		var row = rows[i];
		var up = row.getAttribute(this.upId);
		var id = row.getAttribute(this.selfId);
		var upRow = this.findRow(up);
		if(upRow != null){
			if(upRow.rowIndex > i){
				i--;
			}
			upRow.parentNode.insertBefore(row, upRow.nextSibling);
		}
	}
}


GridTree.prototype.getRows = function(){
	return this.gridTreeObj.rows;
}
GridTree.prototype.isChild = function(id){
	var rows = this.getRows();
	for(var i = this.topIndex; i < rows.length; i++){
		var row = rows[i];
		var up = row.getAttribute(this.upId);
		if(up == id){
			return true;
		}
	}
	return false;
}
GridTree.prototype.findChilds = function(row){
	var rows = this.getRows();
	var arr = new Array();
	var id = row.getAttribute(this.selfId);
	var begin = this.topIndex;
	if(!this.allFind){
		begin = row.rowIndex + 1;
	}
	for(var i = begin; i < rows.length; i++){
		var row = rows[i];
		var up = row.getAttribute(this.upId);
		if(up == id){
			arr.push(row);
		}
	}
	return arr;
}
GridTree.prototype.findRow = function(id){
	var rows = this.getRows();
	for(var i = this.topIndex; i < rows.length; i++){
		var row = rows[i];
		var up = row.getAttribute(this.selfId);
		if(up == id){
			return row;
		}
	}
	return null;
}
GridTree.prototype.outerHTML = function(obj){
	var outerHtml = "";
	if(navigator.appName.indexOf("Explorer") > -1){ 
		imgOut = obj.outerHTML;
	}else{
		imgOut = document.createElement("DIV").appendChild(obj.cloneNode(true)).parentNode.innerHTML; 
	}
	return imgOut;
}